<?php


namespace App\Models;

use App\Models\Concerns\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Drivers extends Model
{
    use SoftDeletes,
        Common;

    protected $table = 'drivers';
    protected $primaryKey = 'id';
    protected $hidden = ['updated_at', 'deleted_at'];

    protected $casts = [
        'identity_photo' => 'array',
        'work_scope' => 'array',
        'licence_photo' => 'array'
    ];


    /**
     * 所属公司
     */
    public function company()
    {
        return $this->belongsTo(Company::class, 'company_id', 'id');
    }
    /**
     * 司机拥有车辆
     */
    public function car()
    {
        return $this->hasManyThrough(
            Car::class,
            DriverCar::class,
            'driver_id',
            'id',
            'id',
            'car_id');
    }


    /**
     * 接单城市
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function city()
    {
        return $this->belongsTo(Region::class, 'city', 'id');
    }
    /**
     * 头像
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function head()
    {
        return $this->hasOne(UploadFile::class, 'id', 'head_image')->select(['id', 'file_url', 'file_name']);
    }

    /**
     * 所属用户
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 代驾
     */
    public function driver_others()
    {
        return $this->hasOne(DriverOthers::class,'driver_id','id');
    }
}
